package nrpc.client.skst;

import java.io.IOException;
import java.nio.channels.SocketChannel;
import java.util.function.Consumer;
import java.util.function.Function;

import nrpc.client.serst.ServiceNode;

/**
 * 一次性Socket策略
 * 该连接管理策略的SocketWrapper每次获取SocketChannel都会新建连接，归还则close该SocketChannel
 */
public class SingleSocketStrategy implements SocketStrategy {

	@Override
	public void disconnect(ServiceNode node) {
	}

	@Override
	public void close() {
	}

//	@Override
//	public boolean getSocket(ServiceNode node, Function<? super SocketChannel, ? extends Boolean> callback) {
//		SocketChannel socketChannel = null;
//		try {
//			socketChannel = SocketChannel.open(node.getAddress());
////			socketChannel.configureBlocking(false);
//			if(!callback.apply(socketChannel)) return false;
//			socketChannel.close();
//		} catch (IOException e) {
////			this.disconnect(node);
//			return false;
//		}
//		return true;
//	}

	@Override
	public SocketChannel getChannel(ServiceNode node) throws IOException {
		SocketChannel socketChannel = SocketChannel.open(node.getAddress());
		return socketChannel;
	}

	@Override
	public void backChannel(ServiceNode node, SocketChannel channel) {
		try {
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//	private final static long TIMEOUT = 10000;

//	@Override
//	public SocketWrapper getSocketWrapper(Service service) {
//		return new SocketWrapper() {
//			@Override
//			public SocketChannel getSocketChannel() {
//				SocketChannel socketChannel = null;
//				try {
//					socketChannel = SocketChannel.open(service.getAddress());
////					socketChannel.configureBlocking(false);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//				return socketChannel;
//			}
//			@Override
//			public void backSocketChannel(SocketChannel socketChannel) {
//				try {
//					socketChannel.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		};
//	}}
	
//	private ServiceStrategy serviceStrategy;
//	private volatile List<SocketWrapper> socketWrappers = new ArrayList<>();
//	
//	public SingleSocketStrategy(ServiceStrategy serviceStrategy) {
//		this.serviceStrategy = serviceStrategy;
//		this.serviceStrategy.subscribe(x->{ // 根据收到的订阅回调消息更新socketWrappers
//			synchronized(SingleSocketStrategy.this){
//				socketWrappers = new ArrayList<>();
//				x.forEach(y->socketWrappers.add(new SocketWrapper() {
//					@Override
//					public SocketChannel getSocketChannel() throws IOException {
//						SocketChannel socketChannel = null;
////							System.out.println(y.getAddress());
//						// 连接失败抛出异常
//						socketChannel = SocketChannel.open(y.getAddress());
////							socketChannel.configureBlocking(false);
//						return socketChannel;
//					}
//					@Override
//					public void backSocketChannel(SocketChannel socketChannel) {
//						try {
//							socketChannel.close();
//						} catch (IOException e) {
//							e.printStackTrace();
//						}
//					}
//				}));
//				if(socketWrappers.size() > 0) SingleSocketStrategy.this.notifyAll();
//			}
//		});
//	}
//	
//	@Override
//	public synchronized List<SocketWrapper> getSocketWrappers() {
////		if(serviceStrategy instanceof SubscribeServiceStrategy s){
////			System.out.println("sub");
////		}
//		while(socketWrappers.size() == 0){
//			try {
//				this.wait(TIMEOUT);
//			} catch (InterruptedException e) {}
//		}
//		return socketWrappers;
//	}
//
//	@Override
//	public synchronized void close() {
//		serviceStrategy.close();
//		this.notifyAll();
//	}

}
